var app = getApp();
// pages/product/vip1.js
//引入这个插件，使html内容自动转换成wxml内容
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guwen:0,
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_settings.html',
      method: 'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        type: 'guwen'
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.ret;
        if (status == 0) {
          var settings = res.data.data;
          WxParse.wxParse('content', 'html', settings.desc, that, 3);

          that.setData({
            settings: settings,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000,
          });
        }
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000,
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onShareAppMessage: function () {
    return {
      title: '赚钱那些事儿',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },

  createVipOrder: function () {
    //创建订单
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/buy_guwen.html',
      method: 'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data; console.log(data);
        if (res.data.ret == 0) {
          //创建订单成功
          wx.showToast({
            title: "下单成功",
            duration: 2000
          });
          that.wxpay(data.data);
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！err:createProductOrder',
          duration: 2000
        });
      }
    });
  },
  //调起微信支付
  wxpay: function (order) {
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/wxpay_guwen.html',
      data: {
        order_id: order.order_id,
        openid: app.globalData.userInfo.openid,
        seesionid: app.globalData.userInfo.sessionId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 0) {
          var order = res.data.data;
          wx.requestPayment({
            timeStamp: order.timeStamp,
            nonceStr: order.nonceStr,
            package: order.package,
            signType: 'MD5',
            paySign: order.paySign,
            success: function (res) {
              wx.showToast({
                title: "支付成功!",
                duration: 2000,
              });
              that.setData({ guwen: 1 });
              
            },
            fail: function (res) {
              console.log(res)
              wx.showToast({
                title: '支付出错',
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！err:wxpay',
          duration: 2000
        });
      }
    })
  },

  test: function (order) {
    var that = this;
    wx.showToast({
      title: "支付成功!",
      duration: 2000,
    });
    //app.globalData.guwen = 1;
    that.setData({guwen:1});
  },
})